package njust.struts2.action;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import njust.dao.GoodsDao;
import njust.dao.MessageDao;
import njust.domain.Goods;
import njust.domain.Message;
import njust.domain.User;

import com.opensymphony.xwork2.ActionSupport;

public class MessageAction extends ActionSupport {

	MessageDao messageDao;
	private GoodsDao goodsDao;
	String msg;
	int userId;
	Message message;

	public String addMessage() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		request.setCharacterEncoding("utf-8");
		response.setContentType("textml;charset=utf-8");
		PrintWriter out = response.getWriter();
		int goodsId=Integer.parseInt(request.getParameter("goodId"));
		System.out.println(goodsId);
		HttpSession session = request.getSession();
		
		User user = (User) session.getAttribute("user");
        System.out.println(user);
        System.out.println(message.getContent());
		if (user == null || message.getContent().equals("")) {
			out.println(0);
			out.flush();
			out.close();
			return null;
		}
		Goods goods=goodsDao.getGoodById(goodsId);
		System.out.println(goods.getGoodname());
		message.setTime(new java.sql.Timestamp(Calendar.getInstance().getTime()
				.getTime()));
		goods.getMessage().add(message);
		message.setUserid(user.getId());
		message.setGoods(goods);
		
		if (messageDao.addMessage(message)) {
			goodsDao.updateGoods(goods);
            out.println(1);
		} else {
            out.println(2);
		}
		return null;
	}

	public String deleteMessage(int id) {
		if (messageDao.deleteMessage(id)) {
			msg = "删除成功";
			return "deletemessageok";
		} else {
			msg = "删除失败";
			return "deletemessageerror";
		}
	}

	public String getAllMessage() {
		List allmsg = new ArrayList();
		allmsg = messageDao.getAllMessage();
		HttpServletRequest re = ServletActionContext.getRequest();
		re.setAttribute("imageList", allmsg);
		return "getallMessageOK";
	}

	public String getMessageById(int id) {
		message = messageDao.getMessageById(id);
		if (message != null)
			return "getMessageOK";
		else
			return "getMessageError";
	}

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	
	public GoodsDao getGoodsDao() {
		return goodsDao;
	}

	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}


	private static final long serialVersionUID = 1L;

}
